import h5py
import numpy as np
import pandas as pd
import netCDF4 as nc

# 定义文件路径
hdf_file_path = r"/home/liudd/data/fy4a_l1/FY4A-_AGRI--_N_DISK_1047E_L1-_FDI-_MULT_NOM_20190228234500_20190228235959_4000M_V0001.HDF"
coord_file_name = '/home/liudd/data_preprocessing/FY4A_coordinates.nc'
output_file = '/home/liudd/deeplearing/backup/result_updata.csv'  # 直接指定输出文件路径

# 加载地理坐标数据
coord_file_open = nc.Dataset(coord_file_name, 'r')
lat = coord_file_open.variables['lat'][:, :].T
lon = coord_file_open.variables['lon'][:, :].T

# 读取 HDF 文件中的数据
NOMNames = [f'NOMChannel{str(i).zfill(2)}' for i in range(1, 15)]
CALNames = [f'CALChannel{str(i).zfill(2)}' for i in range(1, 15)]
img = np.zeros((2748, 2748, 14), dtype=np.float32)

with h5py.File(hdf_file_path, 'r') as h5file:
    for i in range(14):
        NOMData = h5file[NOMNames[i]][:]
        CalData = h5file[CALNames[i]][:]
        # 对 NOMData 进行有效值筛选
        valid_mask = (NOMData >= 0) & (NOMData < 4096)
        if i == 6:  # 对应通道 7 的索引是 6
            valid_mask = (NOMData >= 0) & (NOMData < 65536)
        TOARefData = np.zeros_like(NOMData, dtype=np.float32)
        # 获取有效索引
        indices = np.where(valid_mask)
        # 使用矢量化操作进行辐射定标
        index_values = NOMData[indices].astype(int)
        valid_indices = index_values[(0 <= index_values) & (index_values < len(CalData))]
        # 赋值
        TOARefData[indices] = CalData[valid_indices]
        img[:, :, i] = TOARefData

# 将数据整理成 DataFrame
rows, cols = np.indices(img.shape[:2])

# 初始化数据字典
data = {
    'Latitude': lat.flatten(),
    'Longitude': lon.flatten(),
    'Row': rows.flatten(),
    'Column': cols.flatten(),
}

# 添加每个通道的数据
for i in range(14):
    data[f'ch{i + 1}'] = img[:, :, i].flatten()

df = pd.DataFrame(data)

# 保存结果为 CSV 文件
df.to_csv(output_file, index=False)

print("数据处理完成，已保存为 CSV 文件。")